﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects.BO_GetObjects;
using DigiLib;
using System.Security.Cryptography;

public partial class controls_admin_ArticlesEdit : Admin_Controls_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                base.ModuleByContent(lsbModule, "ARTICLES", 1);
                base.bindDDL(ddlCategories, BO_portal_categories.InsideContent(0, "ARTICLES"), "FULL_CATEGORY_NAME", "CATEGORY_ID");
                loadData();
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
            }
        }
    }

    private void loadData()
    {
        DataRow drow = BO_portal_articles.Search(Id).Rows[0];
        ddlCategories.SelectedValue = drow["CATEGORY_ID"].ToString();
        ctImages.Src = "/" + Setting.Path + drow["THUMB_IMG"].ToString();
        txtFULL_ARTICLE_TITLE.Text = drow["FULL_ARTICLE_TITLE"].ToString();
        txtFULL_ARTICLE_DESCRIPTION.Text = drow["FULL_ARTICLE_DESCRIPTION"].ToString();
        ckContent.Text = drow["ARTICLE_DETAIL"].ToString();
        txtMetaKeyword.Text = drow["META_KEYWORD"].ToString();
        txtMetaDescription.Text = drow["META_DESCRIPTION"].ToString();
        txtTags.Text = drow["TAGS"].ToString();
        cboDisplay.Checked = Convert.ToBoolean(drow["IS_DISPLAY_ON_HOME_PAGE"].ToString());
        cboHot.Checked = Convert.ToBoolean(drow["IS_HOT"].ToString());
        ObjDT = BO_cms.Join_Tables_Select(Id, "FK_portal_articles_portal_modules", false);
        foreach (DataRow dr in ObjDT.Rows)
        {
            foreach (ListItem item in lsbModule.Items)
            {
                if (dr["ID_2"].ToString().Trim().Equals(item.Value.Trim()))
                    item.Selected = true;
                if (Convert.ToInt32(dr["ID_2"]) == Module_ID)
                    txtSortDisplay.Text = dr["VAR1"].ToString();
            }
        }
        ObjDT = BO_portal_articles_related.Select(Id);
        string related = string.Empty;
        foreach (DataRow dr in ObjDT.Rows)
        {
            related += dr["FULL_ARTICLE_TITLE"].ToString() + ", ";
        }
        txtRelated.Text = related;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //try
        //{
        //    DataRow drow = BO_portal_articles.Search(Id).Rows[0];
        //    string default_string = string.Empty;
        //    string filename = drow["THUMB_IMG"].ToString();
        //    filename = base.Upload("articles", filename, fulTHUMB_IMG, divMessage, false);
        //    int category_id = Convert.ToInt32(ddlCategories.SelectedValue);
        //    int status = 1;
        //    string tags = txtTags.Text.Trim();
        //    string related = txtRelated.Text.Trim();
        //    string title = txtFULL_ARTICLE_TITLE.Text.Trim();
        //    string full_article_description = txtFULL_ARTICLE_DESCRIPTION.Text.Trim();
        //    string details = ckContent.Text;
        //    bool displayOnHome = cboDisplay.Checked;
        //    bool hot = cboHot.Checked;
        //    int sort_display = Convert.ToInt32(txtSortDisplay.Text.Trim());
        //    string m_key = txtMetaKeyword.Text.Trim();
        //    string m_desc = txtMetaDescription.Text.Trim();
        //    string code = drow["ARTICLE_CODE"].ToString();
        //    string strResult = BO_portal_articles.Update(Id, category_id, 1, code,
        //                     default_string, filename, string.Empty, string.Empty, title, string.Empty, string.Empty
        //                     , string.Empty, full_article_description, string.Empty,
        //                     details, string.Empty, m_key, m_desc, tags, Convert.ToInt32(drow["TOTAL_DOWNLOAD"]),
        //                    Convert.ToInt32(drow["TOTAL_VOTE"]), Convert.ToInt32(drow["TOTAL_COMMENT"]), Convert.ToInt32(drow["TOTAL_VIEW"]),
        //                    displayOnHome, hot, sort_display, sort_display, Convert.ToDateTime(drow["PUBLISHED_DATE"]), Convert.ToDateTime(drow["APPROVED_DATE"]),
        //                    Administrator.USER_NAME, Convert.ToDateTime(drow["CREATED_DATE"]), Administrator.USER_NAME, DateTime.Now, Administrator.USER_NAME, status);
        //    if (strResult != "0")
        //    {
        //        divMessage.InnerHtml = MessageBox.Success("Thông Báo", "Cập nhật thành công!");
        //        foreach (ListItem item in lsbModule.Items)
        //        {
        //            if (item.Selected)
        //            {
        //                if (Module_ID == Convert.ToInt32(item.Value))
        //                {
        //                    BO_cms.Join_Tables_Update(Id, Module_ID, "FK_portal_articles_portal_modules", sort_display, 1);
        //                }
        //                else BO_cms.Join_Tables_Insert(Id, Convert.ToInt32(item.Value), "FK_portal_articles_portal_modules", sort_display, 1);
        //            }
        //            else
        //            {
        //                BO_cms.Join_Tables_Delete(Id, Convert.ToInt32(item.Value), "FK_portal_articles_portal_modules");
        //            }
        //        }
        //        foreach (string tag in tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        //        {
        //            if (!string.IsNullOrEmpty(tag))
        //            {
        //                try
        //                {
        //                    int id_1 = Convert.ToInt32(BO_portal_tags.Insert(tag.Trim(), 0, DateTime.Now, Administrator.USER_NAME, DateTime.Now, Administrator.USER_NAME, true));
        //                    if (id_1 > 0) BO_cms.Join_Tables_Insert(id_1, Id, "FK_portal_tags_portal_articles", 0, 0);
        //                }
        //                catch { continue; }
        //            }
        //        }
        //        int article_related;
        //        string indellist = string.Empty;
        //        foreach (string rela in related.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        //        {
        //            try
        //            {
        //                article_related = Convert.ToInt32(BO_portal_articles.Search(rela, 1).Rows[0]["ARTICLE_ID"]);
        //                BO_portal_articles_related.Update(Convert.ToInt32(strResult), article_related);
        //                indellist += article_related.ToString() + ";";
        //            }
        //            catch { continue; }
        //        }
        //        BO_portal_articles_related.Delete(Convert.ToInt32(strResult), indellist);
        //        if (Module_ID > 0)
        //            Response.Redirect("default.aspx?control=articlesmodule&module_id=" + Module_ID.ToString() + "&keyword=" + Keyword);
        //        else
        //            Response.Redirect("default.aspx?control=articles&category_id=" + ddlCategories.SelectedValue + "&" + Request["pager"].ToString() + "=" + Request[Request["pager"].ToString()].ToString() + "&keyword=" + Keyword);
        //    }
        //    else
        //        divMessage.InnerHtml = MessageBox.Error("Lỗi", "Có lỗi trong quá trình xử lý! Đăng tin thất bại!");
        //}
        //catch (Exception ex)
        //{
        //    divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
        //}
        //    try
        //    {
        //        DataRow drow = BO_portal_articles.Search(Id).Rows[0];
        //        string default_string = string.Empty;
        //        string filename = drow["THUMB_IMG"].ToString();
        //        filename = base.Upload("articles", filename, fulTHUMB_IMG, divMessage, false);
        //        int category_id = Convert.ToInt32(ddlCategories.SelectedValue);
        //        int status = 1;
        //        string tags = txtTags.Text.Trim();
        //        string related = txtRelated.Text.Trim();
        //        string title = txtFULL_ARTICLE_TITLE.Text.Trim();
        //        string full_article_description = txtFULL_ARTICLE_DESCRIPTION.Text.Trim();
        //        string details = ckContent.Text;
        //        bool displayOnHome = cboDisplay.Checked;
        //        bool hot = cboHot.Checked;
        //        int sort_display = Convert.ToInt32(txtSortDisplay.Text.Trim());
        //        string m_key = txtMetaKeyword.Text.Trim();
        //        string m_desc = txtMetaDescription.Text.Trim();
        //        string code = drow["ARTICLE_CODE"].ToString();
        //        string strResult = BO_portal_articles.Update(Id, category_id, 1, code,
        //                         default_string, filename, string.Empty, string.Empty, title, string.Empty, string.Empty
        //                         , string.Empty, full_article_description, string.Empty,
        //                         details, string.Empty, m_key, m_desc, tags, Convert.ToInt32(drow["TOTAL_DOWNLOAD"]),
        //                        Convert.ToInt32(drow["TOTAL_VOTE"]), Convert.ToInt32(drow["TOTAL_COMMENT"]), Convert.ToInt32(drow["TOTAL_VIEW"]),
        //                        displayOnHome, hot, sort_display, sort_display, Convert.ToDateTime(drow["PUBLISHED_DATE"]), Convert.ToDateTime(drow["APPROVED_DATE"]),
        //                        Administrator.USER_NAME, Convert.ToDateTime(drow["CREATED_DATE"]), Administrator.USER_NAME, DateTime.Now, Administrator.USER_NAME, status);
        //        if (strResult != "0")
        //        {
        //            divMessage.InnerHtml = MessageBox.Success("Thông Báo", "Cập nhật thành công!");
        //            foreach (ListItem item in lsbModule.Items)
        //            {
        //                if (item.Selected)
        //                {
        //                    if (Module_ID == Convert.ToInt32(item.Value))
        //                    {
        //                        BO_cms.Join_Tables_Update(Id, Module_ID, "FK_portal_articles_portal_modules", sort_display, 1);
        //                    }
        //                    else BO_cms.Join_Tables_Insert(Id, Convert.ToInt32(item.Value), "FK_portal_articles_portal_modules", sort_display, 1);
        //                }
        //                else
        //                {
        //                    BO_cms.Join_Tables_Delete(Id, Convert.ToInt32(item.Value), "FK_portal_articles_portal_modules");
        //                }
        //            }
        //            foreach (string tag in tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        //            {
        //                if (!string.IsNullOrEmpty(tag))
        //                {
        //                    try
        //                    {
        //                        int id_1 = Convert.ToInt32(BO_portal_tags.Insert(tag.Trim(), 0, DateTime.Now, Administrator.USER_NAME, DateTime.Now, Administrator.USER_NAME, true));
        //                        if (id_1 > 0) BO_cms.Join_Tables_Insert(id_1, Id, "FK_portal_tags_portal_articles", 0, 0);
        //                    }
        //                    catch { continue; }
        //                }
        //            }
        //            int article_related;
        //            string indellist = string.Empty;
        //            foreach (string rela in related.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        //            {
        //                try
        //                {
        //                    article_related = Convert.ToInt32(BO_portal_articles.Search(rela, 1).Rows[0]["ARTICLE_ID"]);
        //                    BO_portal_articles_related.Update(Convert.ToInt32(strResult), article_related);
        //                    indellist += article_related.ToString() + ";";
        //                }
        //                catch { continue; }
        //            }
        //            BO_portal_articles_related.Delete(Convert.ToInt32(strResult), indellist);
        //            if (Module_ID > 0)
        //                Response.Redirect("default.aspx?control=articlesmodule&module_id=" + Module_ID.ToString() + "&keyword=" + Keyword);
        //            else
        //                Response.Redirect("default.aspx?control=articles&category_id=" + ddlCategories.SelectedValue + "&" + Request["pager"].ToString() + "=" + Request[Request["pager"].ToString()].ToString() + "&keyword=" + Keyword);
        //        }
        //        else
        //            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Có lỗi trong quá trình xử lý! Đăng tin thất bại!");
        //    }
        //    catch (Exception ex)
        //    {
        //        divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
        //    }
    }
}
